package com.mypage.view.datedialog;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.mypage.view.datedialog.DateTimePopupWindow.Type;

public class DateWheelTime {

	@SuppressLint("SimpleDateFormat")
	public static DateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");
	private View view;
	private DateWheelView wv_year;
	private DateWheelView wv_month;
	private DateWheelView wv_day;
	private DateWheelView wv_hours;
	private DateWheelView wv_mins;
	public int screenheight;
	private Type type;
	private String mEn = RunTimeManager.getInstance().getlanguage();
	private static int START_YEAR = 1990, END_YEAR = 2100;

	public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

	public static int getSTART_YEAR() {
		return START_YEAR;
	}

	public static void setSTART_YEAR(int sTART_YEAR) {
		START_YEAR = sTART_YEAR;
	}

	public static int getEND_YEAR() {
		return END_YEAR;
	}

	public static void setEND_YEAR(int eND_YEAR) {
		END_YEAR = eND_YEAR;
	}

	public DateWheelTime(View view) {
		super();
		this.view = view;
		type = Type.ALL;
		setView(view);
	}

	public DateWheelTime(View view, Type type) {
		super();
		this.view = view;
		this.type = type;
		setView(view);
	}

	public void setPicker(int year, int month, int day) {
		this.setPicker(year, month, day, 0, 0);
	}

	/**
	 * @Description: ��������ʱ��ѡ����
	 */
	public void setPicker(int year, int month, int day, int h, int m) {
		// ��Ӵ�С���·ݲ�����ת��Ϊlist,����֮����ж�
		String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
		String[] months_little = { "4", "6", "9", "11" };

		final List<String> list_big = Arrays.asList(months_big);
		final List<String> list_little = Arrays.asList(months_little);

		Context context = view.getContext();
		// ��
		wv_year = (DateWheelView) view.findViewById(R.id.year);
		wv_year.setAdapter(new DateNumericWheelAdapter(START_YEAR, END_YEAR));// ����"��"����ʾ���
		/*if("en".equals(mEn)){
			wv_year.setLabel(context.getString(R.string.pickerview_year_en));// �������
		}else{
			wv_year.setLabel(context.getString(R.string.pickerview_year));// �������	
		}*/
		
		wv_year.setCurrentItem(year - START_YEAR);// ��ʼ��ʱ��ʾ�����

		// ��
		wv_month = (DateWheelView) view.findViewById(R.id.month);
		wv_month.setAdapter(new DateNumericWheelAdapter(1, 12));
		/*if("en".equals(mEn)){
			wv_month.setLabel(context.getString(R.string.pickerview_month_en));
		}else{
			wv_month.setLabel(context.getString(R.string.pickerview_month));	
		}*/
		
		wv_month.setCurrentItem(month);

		// ��
		wv_day = (DateWheelView) view.findViewById(R.id.day);
		// �жϴ�С�¼��Ƿ�����,����ȷ��"��"�����
		if (list_big.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new DateNumericWheelAdapter(1, 31));
		} else if (list_little.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new DateNumericWheelAdapter(1, 30));
		} else {
			// ����
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				wv_day.setAdapter(new DateNumericWheelAdapter(1, 29));
			else
				wv_day.setAdapter(new DateNumericWheelAdapter(1, 28));
		}
		/*if("en".equals(mEn)){
			wv_day.setLabel(context.getString(R.string.pickerview_day_en));	
		}else{
			wv_day.setLabel(context.getString(R.string.pickerview_day));	
		}*/
		
		wv_day.setCurrentItem(day - 1);

		wv_hours = (DateWheelView) view.findViewById(R.id.hour);
		wv_hours.setAdapter(new DateNumericWheelAdapter(0, 23));
//		if("en".equals(mEn)){
//			wv_hours.setLabel(context.getString(R.string.pickerview_hours_en));// �������
//		}else{
			wv_hours.setLabel(context.getString(R.string.pickerview_hours));// �������	
//		}
		
		wv_hours.setCurrentItem(h);

		wv_mins = (DateWheelView) view.findViewById(R.id.min);
		wv_mins.setAdapter(new DateNumericWheelAdapter(0, 59));
//		if("en".equals(mEn)){
//			wv_mins.setLabel(context.getString(R.string.pickerview_minutes_en));// �������
//		}else{
			wv_mins.setLabel(context.getString(R.string.pickerview_minutes));// �������
//		}
		
		wv_mins.setCurrentItem(m);

		// ���"��"����
		DateOnWheelChangedListener wheelListener_year = new DateOnWheelChangedListener() {
			@Override
			public void onChanged(DateWheelView wheel, int oldValue, int newValue) {
				int year_num = newValue + START_YEAR;
				// �жϴ�С�¼��Ƿ�����,����ȷ��"��"�����
				int maxItem = 30;
				if (list_big
						.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
					wv_day.setAdapter(new DateNumericWheelAdapter(1, 31));
					maxItem = 31;
				} else if (list_little.contains(String.valueOf(wv_month
						.getCurrentItem() + 1))) {
					wv_day.setAdapter(new DateNumericWheelAdapter(1, 30));
					maxItem = 30;
				} else {
					if ((year_num % 4 == 0 && year_num % 100 != 0)
							|| year_num % 400 == 0) {
						wv_day.setAdapter(new DateNumericWheelAdapter(1, 29));
						maxItem = 29;
					} else {
						wv_day.setAdapter(new DateNumericWheelAdapter(1, 28));
						maxItem = 28;
					}
				}
				if (wv_day.getCurrentItem() > maxItem - 1) {
					wv_day.setCurrentItem(maxItem - 1);
				}
			}
		};
		// ���"��"����
		DateOnWheelChangedListener wheelListener_month = new DateOnWheelChangedListener() {
			@Override
			public void onChanged(DateWheelView wheel, int oldValue, int newValue) {
				int month_num = newValue + 1;
				int maxItem = 30;
				// �жϴ�С�¼��Ƿ�����,����ȷ��"��"�����
				if (list_big.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new DateNumericWheelAdapter(1, 31));
					maxItem = 31;
				} else if (list_little.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new DateNumericWheelAdapter(1, 30));
					maxItem = 30;
				} else {
					if (((wv_year.getCurrentItem() + START_YEAR) % 4 == 0 && (wv_year
							.getCurrentItem() + START_YEAR) % 100 != 0)
							|| (wv_year.getCurrentItem() + START_YEAR) % 400 == 0) {
						wv_day.setAdapter(new DateNumericWheelAdapter(1, 29));
						maxItem = 29;
					} else {
						wv_day.setAdapter(new DateNumericWheelAdapter(1, 28));
						maxItem = 28;
					}
				}
				if (wv_day.getCurrentItem() > maxItem - 1) {
					wv_day.setCurrentItem(maxItem - 1);
				}

			}
		};
		wv_year.addChangingListener(wheelListener_year);
		wv_month.addChangingListener(wheelListener_month);

		// �����Ļ�ܶ���ָ��ѡ��������Ĵ�С(��ͬ��Ļ���ܲ�ͬ)
		int textSize = 0;
		switch (type) {
		case ALL:
			textSize = (screenheight / 100) * 3;
			break;
		case YEAR_MONTH_DAY:
			textSize = (screenheight / 100) * 4;
			wv_hours.setVisibility(View.GONE);
			wv_mins.setVisibility(View.GONE);
			break;
		case HOURS_MINS:
			textSize = (screenheight / 100) * 4;
			wv_year.setVisibility(View.GONE);
			wv_month.setVisibility(View.GONE);
			wv_day.setVisibility(View.GONE);
			break;
		case MONTH_DAY_HOUR_MIN:
			textSize = (screenheight / 100) * 3;
			wv_year.setVisibility(View.GONE);
			break;
		case YEAR_MONTH_DAY_HOURS:
			textSize = (screenheight / 100) * 3;
			wv_mins.setVisibility(View.GONE);
			break;
		}

		wv_day.TEXT_SIZE = textSize;
		wv_month.TEXT_SIZE = textSize;
		wv_year.TEXT_SIZE = textSize;
		wv_hours.TEXT_SIZE = textSize;
		wv_mins.TEXT_SIZE = textSize;

	}

	/**
	 * �����Ƿ�ѭ������
	 * 
	 * @param cyclic
	 */
	public void setCyclic(boolean cyclic) {
		wv_year.setCyclic(cyclic);
		wv_month.setCyclic(cyclic);
		wv_day.setCyclic(cyclic);
		wv_hours.setCyclic(cyclic);
		wv_mins.setCyclic(cyclic);
	}

	public String getTime() {
		StringBuffer sb = new StringBuffer();

		switch (type) {
		case ALL:
			sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-")
					.append((wv_month.getCurrentItem() + 1)).append("-")
					.append((wv_day.getCurrentItem() + 1)).append(" ")
					.append(wv_hours.getCurrentItem()).append(":")
					.append(wv_mins.getCurrentItem());
			break;
		case YEAR_MONTH_DAY:
			sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-")
					.append((wv_month.getCurrentItem() + 1)).append("-")
					.append((wv_day.getCurrentItem() + 1)).append(" ")
					.append(wv_hours.getCurrentItem()).append(":")
					.append(wv_mins.getCurrentItem());
			break;
		case HOURS_MINS:
			sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-")
					.append((wv_month.getCurrentItem() + 1)).append("-")
					.append((wv_day.getCurrentItem() + 1)).append(" ")
					.append(wv_hours.getCurrentItem()).append(":")
					.append(wv_mins.getCurrentItem());
			break;
		case MONTH_DAY_HOUR_MIN:
			sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-")
					.append((wv_month.getCurrentItem() + 1)).append("-")
					.append((wv_day.getCurrentItem() + 1)).append(" ")
					.append(wv_hours.getCurrentItem()).append(":")
					.append(wv_mins.getCurrentItem());
			break;
		case YEAR_MONTH_DAY_HOURS:
			sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-")
					.append((wv_month.getCurrentItem() + 1)).append("-")
					.append((wv_day.getCurrentItem() + 1)).append(" ")
					.append(wv_hours.getCurrentItem()).append(":").append(0);
			break;
		}
		return sb.toString();
	}

}
